package com.jx.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.Fraction;

import com.jx.dao.FindFuncByDidDao;
import com.jx.daoimpl.FindFuncByDidDaoimpl;
import com.jx.model.Dep;
import com.jx.service.UpdateDepService;
import com.jx.servicrimpl.UpdateDepServiceImpl;
/**
 * 更新部门信息
 * @author zzzzzzz
 *
 */
public class UpdateDepController extends HttpServlet{
	UpdateDepService uds=new UpdateDepServiceImpl();
	FindFuncByDidDao ffbdd=new FindFuncByDidDaoimpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		//获取前台的数据
		int did=Integer.parseInt(req.getParameter("did"));
		String dname=req.getParameter("dname");
		String ddes=req.getParameter("ddes");
		String[] dfuncs=req.getParameterValues("func");
		
		//查找到当前选中部门对应的功能并放置在session中
		req.getSession().setAttribute("currentdepfunc",ffbdd.findFuncByDid(did) );
		//拼接部门功能字符串
		String dfunc="";
		for (int i = 0; i < dfuncs.length; i++) {
			
			if(i != dfuncs.length-1){
				dfunc+=dfuncs[i]+",";
			}else{
				dfunc+=dfuncs[i];
			}
		}
		//实例化部门
		Dep dep=new Dep(did,dname,ddes,dfunc);
		//更新部门
		boolean result=uds.updateDepService(dep);
		//传回数据
		String j="0";
		if(result==true){
			j="1";
		}
		PrintWriter pw=resp.getWriter();
		pw.write(j);
	}
}
